<?php
namespace Common\Model;
use Think\Model;
/**
 * Class HomepageblockModel
 * @package Common\Model
 * 菜单模型
 */
class NavModel extends BaseModel{

    public $pix = "";
    public $fieldsStr = '*';
    public $orderStr = 'sort asc';

    public function get_name($id = 0){
        $map['id'] = array('eq', $id);
        $one = $this->where($map)->find();
        return $one?$one['title']:false;
    }

    private function has_show_child($pid){
        $map['parent_id'] = array('eq', $pid);
        $map['istofront'] = array('eq', 1);
        $one = $this->where($map)->find();
        return $one?$one:false;
    }

    private function has_child($pid){
        $map['parent_id'] = array('eq', $pid);
        $one = $this->where($map)->find();
        return $one?$one:false;
    }

    private function query_show($pid){
        $map['parent_id'] = array('eq', $pid);
        $map['istofront'] = array('eq', 1);
        $list = $this->where($map)->select();
        return $list?$list:false;
    }

    public function get_all_show_nav($pid = 0, $jinx = ''){
        $list = '';
        $return = array();
        if($this->has_show_child($pid)){
            $list = $this->query_show($pid);
        }
        foreach($list as $nav){
            $return[] = array('id'=>$nav['id'], 'name'=>$jinx.'├─'.$nav['title']);
            if($this->has_show_child($nav['id'])){
                $return = array_merge($return, $this->get_all_show_nav($nav['id'], '&nbsp;'.$jinx));
            }

        }
        return $return;
    }

    public function get_all_nav($pid = 0, $jinx = ''){
        $list = '';
        $return = array();
        if($this->has_child($pid)){
            $list = $this->queryField('parent_id', $pid);
        }
        foreach($list as $nav){
            $return[] = array('id'=>$nav['id'], 'name'=>$jinx.'├─'.$nav['title']);
            if($this->has_child($nav['id'])){
                $return = array_merge($return, $this->get_all_nav($nav['id'], '&nbsp;'.$jinx));
            }

        }
        return $return;
    }

    public function get_all($pid = 0, $jinx = ''){
        $list = '';
        $return = array();
        if($this->has_child($pid)){
            $list = $this->queryField('parent_id', $pid);
        }
        foreach($list as $nav){
            $nav['title'] = $jinx.'├─'.$nav['title'];
            $return[] = $nav;
            if($this->has_child($nav['id'])){
                $return = array_merge($return, $this->get_all($nav['id'], LANG('eh_xnm').$jinx));
            }

        }
        return $return;
    }

    public function get_all_text($pid = 0){
        $text = '';
        if($this->has_child($pid)){
            $list = $this->queryField('parent_id', $pid);
        }
        foreach($list as $nav){
            $text .= '<li>';
            $text .= '<span><a target="content_frame" href="'.$nav['admin_url'].'">'.$nav['title'].'</a></span>';
            if($this->has_child($nav['id'])){
                $text .= '<ul>';
                $text .= $this->get_all_text($nav['id']);
                $text .= '</ul>';
            }
            $text .= '</li>';
        }
        return $text;
    }

    public function del($ids){
        foreach($ids as $id){
            $this->del_by_pid($id);
        }
    }

    private function del_by_pid($pid){
        if($this->has_child($pid)){
            $list = $this->queryField('parent_id', $pid);
        }
        foreach($list as $nav){
            if($this->has_child($nav['id'])){
                $this->del_by_pid($nav['id']);
            }
            $this->delete($nav['id']);
        }
        $this->delete($pid);
    }
}